//
//  Team9ExpertTableViewCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/12.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertTableViewCell.h"
#import <Masonry.h>
#import "Team9BaseLabel.h"
#import "Team9BaseView.h"
#import "Team9DesignerModel.h"
#import <UIImageView+WebCache.h>

@interface Team9ExpertTableViewCell ()

@property(nonatomic,strong)Team9BaseView *myBackView;
@property(nonatomic,strong)UIImageView *imageViewface;
@property(nonatomic,strong)Team9BaseLabel *labelName;
@property(nonatomic,strong)Team9BaseLabel *labelAddress;
@property(nonatomic,strong)Team9BaseLabel *labelPrice;

@property(nonatomic, strong)NSMutableArray<UIImageView *> *imageViews;
@property(nonatomic,strong)UIView *viewLine;
//已验证,预约
@property(nonatomic,strong)Team9BaseLabel *labelAlready;
@property(nonatomic,strong)Team9BaseLabel *labelAppointment;

@end
@implementation Team9ExpertTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark init method
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)createView{
//    self.contentView.backgroundColor = [UIColor lightGrayColor];
    
    self.myBackView = [[Team9BaseView alloc] init];
    [self.contentView addSubview:self.myBackView];
    
    self.imageViewface = [UIImageView new];
    [self.contentView addSubview:self.imageViewface];
    
    self.labelName = [[Team9BaseLabel alloc] initWith:LableColorTypeOrangeGreen];
    [self.contentView addSubview:self.labelName];
    
    self.labelAddress = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.labelAddress];
    
    self.labelPrice = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.labelPrice];
    
    self.labelAlready = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.labelAlready];
    
    self.labelAppointment = [[Team9BaseLabel alloc] init];
    [self.contentView addSubview:self.labelAppointment];
    
    self.viewLine = [UIView new];
    [self.contentView addSubview:self.viewLine];
    
    self.imageViews = @[].mutableCopy;
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [UIImageView new];
        [self.imageViews addObject:imageView];
        [self.contentView addSubview:imageView];
//        imageView.backgroundColor = [UIColor yellowColor];
    }
    self.imageViewface.layer.cornerRadius = WIDTH / 375 * 20;
    self.imageViewface.layer.masksToBounds = YES;
    self.labelName.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.labelAddress.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.myBackView.layer.cornerRadius = 5;
    self.labelAlready.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    self.labelAppointment.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    self.labelAppointment.textAlignment = NSTextAlignmentRight;
    self.viewLine.backgroundColor = [UIColor lightGrayColor];
    self.labelPrice.textAlignment = NSTextAlignmentRight;
    self.contentView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    [self p_contrians];
}
- (void)setModel:(Team9DesignerModel *)model
{
    if (_model != model) {
        _model = model;
        
    }
    self.labelName.text = self.model.user_nick;
    self.labelAddress.text = self.model.location;
    [self.imageViewface sd_setImageWithURL:[NSURL URLWithString:self.model.user_face] placeholderImage:[UIImage imageNamed:@"333"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [UIView animateWithDuration:0.3 animations:^{
            CATransition *transition = [CATransition animation];
            transition.type = @"fade";
            [transition setDuration:0.5];
            [transition setRepeatCount:1];
            // 向imageView上添加动画效果
            [self.imageViewface.layer addAnimation:transition forKey:@"transition"];
        }];
        
    }];
    
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = self.imageViews[i];
        if (self.model.pro_covers.count > i) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.pro_covers[i]] placeholderImage:[UIImage imageNamed:@"111"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                [UIView animateWithDuration:0.3 animations:^{
                    CATransition *transition = [CATransition animation];
                    transition.type = @"fade";
                    [transition setDuration:0.5];
                    [transition setRepeatCount:1];
                    // 向imageView上添加动画效果
                    [imageView.layer addAnimation:transition forKey:@"transition"];
                }];
            }];
        }
    }
    
    self.labelPrice.text = [NSString stringWithFormat:@"%@-%@/平米", self.model.pro_price_min, self.model.pro_price_max];
    
    
    self.labelAlready.text = [NSString stringWithFormat:@"%@套已验证作品", self.model.contacted_count];
    self.labelAppointment.text = [NSString stringWithFormat:@"%@人预约过", self.model.verified_work_count];
    [self changecolor];
}
- (void)layoutSubviews
{
    [super layoutSubviews];

}
- (void)changecolor
{
    [super changecolor];
    if (!self.labelAlready.text || !self.labelAppointment.text) {
        return;
    }
    NSMutableAttributedString *attributeStringReady =
    [[NSMutableAttributedString alloc] initWithString:self.labelAlready.text];
    NSMutableAttributedString *attributeStringAppointment =
    [[NSMutableAttributedString alloc] initWithString:self.labelAppointment.text];
    
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor cyanColor];
    } else color = [UIColor redColor];

    [attributeStringReady setAttributes:@{
                                          NSForegroundColorAttributeName : color
                                          } range:[self.labelAlready.text rangeOfString:self.model.contacted_count]];
    [attributeStringAppointment setAttributes:@{
                                                NSForegroundColorAttributeName : color}
                                        range:[self.labelAppointment.text rangeOfString:self.model.verified_work_count]];
    
    self.labelAppointment.attributedText = attributeStringAppointment;
    self.labelAlready.attributedText = attributeStringReady;
    
}
- (void)p_contrians{
    [self.myBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    [self.imageViewface mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myBackView).offset(10);
        make.left.equalTo(self.myBackView).offset(10);
        make.width.equalTo(@(WIDTH / 375 * 40));
        make.height.equalTo(self.imageViewface.mas_width);
        
    }];
    [self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageViewface);
        make.left.equalTo(self.imageViewface.mas_right).offset(10);
        make.right.equalTo(self.mas_centerX);
    }];
    [self.labelAddress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.imageViewface);
        make.left.equalTo(self.imageViewface.mas_right).offset(10);
        make.right.equalTo(self.mas_centerX);
    }];
    
    
    UIImageView *lastView = nil;
    for (UIImageView *imageView in self.imageViews) {
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.imageViewface.mas_bottom).offset(10);
            if (lastView) {
                make.left.equalTo(lastView.mas_right).offset(1);
            } else make.left.equalTo(self.myBackView).offset(10);
            make.width.equalTo(@((WIDTH - 3 - 40) / 4));
            make.height.equalTo(imageView.mas_width);;
        }];
        lastView = imageView;
    }
    
    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        UIImageView *imageView = self.imageViews[0];
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.centerX.equalTo(self.mas_centerX);
        make.width.equalTo(@(1));
        make.height.equalTo(self.labelAlready.mas_height);
    }];
    
    [self.labelAlready mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewLine);
        make.left.equalTo(self.viewLine).offset(5);
        make.right.equalTo(self.myBackView).offset(-10);
    }];
    
    [self.labelAppointment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.viewLine);
        make.right.equalTo(self.viewLine).offset(-5);
        make.left.equalTo(self.myBackView).offset(10);
        
    }];
    
    [self.labelPrice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.imageViewface);
        make.right.equalTo(self.myBackView).offset(-10);
        make.left.equalTo(self.mas_centerX);
    }];

}
@end
